﻿//------------------------------------------------------------------------------
// The contents of this file are subject to the Microsoft Public License (Ms-PL)
// You may obtain a copy of the License at http://psadmin.codeplex.com/license. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, 
// WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations 
// under the License.
// 
// The initial developer is Ben Foster (ben.foster@retroviz.com)
// Copyright (c) Retroviz Limited (http://www.retroviz.com).
// All Rights Reserved.
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PSAdmin.Core.UI;

namespace PSAdmin.Web
{
    public static class CheckBoxListExtensions
    {
        public static MvcHtmlString CheckBoxList<TValue>(this HtmlHelper helper,
            string name, IEnumerable<KeyValuePair<string, TValue>> items, 
            IEnumerable<TValue> selectedValues)  {
            var checkBoxListItems = from i in items
                                    select new CheckBoxListItem<TValue> {
                                            Text = i.Key,
                                            Value = i.Value,
                                            Selected = selectedValues != null && selectedValues.Contains(i.Value)
                                        };
            return CheckBoxList<TValue>(helper, name, checkBoxListItems);
        }

        public static MvcHtmlString CheckBoxList<TValue>(this HtmlHelper helper, string name, IEnumerable<CheckBoxListItem<TValue>> checkboxListItems) {
            return CheckBoxList(helper, name, checkboxListItems, null);
        }

        public static MvcHtmlString CheckBoxList<TValue>(this HtmlHelper helper, string name,
            IEnumerable<CheckBoxListItem<TValue>> checkBoxListItems, IDictionary<string, object> htmlAttributes)
        {
            var ul = new TagBuilder("ul");

            foreach (var listItem in checkBoxListItems)
            {
                var li = new TagBuilder("li");
                var span = new TagBuilder("span");

                // Set the attributes
                var checkbox = new TagBuilder("input");
                checkbox.MergeAttribute("type", "checkbox");
                checkbox.MergeAttribute("name", name);
                checkbox.MergeAttribute("value", listItem.Value.ToString());

                if (listItem.Selected)
                    checkbox.MergeAttribute("checked", "checked");

                if (htmlAttributes != null)
                    checkbox.MergeAttributes(htmlAttributes, true);

                // Render the tags
                li.InnerHtml = checkbox.ToString(TagRenderMode.SelfClosing);
                span.SetInnerText(listItem.Text);
                li.InnerHtml += span.ToString();

                // Add to the ul tag
                ul.InnerHtml += li.ToString();
            }

            // Return as an MvcHtmlString
            return MvcHtmlString.Create(ul.ToString());
        }
    }
}
